package io.renren.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.app.config.WebMvcConfig;
import io.renren.modules.app.dao.BsMemberDao;
import io.renren.modules.app.entity.BsMemberEntity;
import io.renren.modules.app.service.BsMemberService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Pattern;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;


@Service("bsMemberService")
@Slf4j
public class BsMemberServiceImpl extends ServiceImpl<BsMemberDao, BsMemberEntity> implements BsMemberService {

    @Resource
    private BsMemberDao bsMemberDao ;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        LambdaQueryWrapper<BsMemberEntity> wrapper = new LambdaQueryWrapper<>();
        if (params.get("memberName") != null && !params.get("memberName").toString().equals("")){
            wrapper.like(BsMemberEntity::getMemberName,params.get("memberName"));
        }
        if (params.get("memberSex") != null && !params.get("memberSex").toString().equals("")){
            wrapper.eq(BsMemberEntity::getMemberSex,params.get("memberSex"));
        }
        if (params.get("memberAgeBegin") != null && !params.get("memberAgeBegin").toString().equals("")){
            wrapper.ge(BsMemberEntity::getMemberAge,params.get("memberAgeBegin"));
        }
        if (params.get("memberAgeEnd") != null && !params.get("memberAgeEnd").toString().equals("")){
            wrapper.le(BsMemberEntity::getMemberAge,params.get("memberAgeEnd"));
        }
        IPage<BsMemberEntity> page = this.page(
                new Query<BsMemberEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    //方法上加synchronized 同步监视器是为了防止并发情况发生，从而造成会员编号重复
    @Override
    public synchronized void savaMember(BsMemberEntity bsMember) {
        try {
            //非空性校验
            if (StringUtils.isBlank(bsMember.getMemberName())){
                throw new RRException("会员姓名不能为空");
            }
            if (StringUtils.isBlank(bsMember.getMemberPhone())){
                throw new RRException("会员电话不能为空");
            }
            if (StringUtils.isBlank(bsMember.getMemberSex())){
                throw new RRException("会员性别不能为空");
            }
           if (bsMember.getMemberAge() == null){
               throw new RRException("会员年龄不能为空");
           }
           if (StringUtils.isBlank(bsMember.getMemberIdnum())){
               throw new RRException("会员身份证号不能为空");
           }
           if (bsMember.getMemberIdnum().length() != 18){
               throw new RRException("身份证号长度不合法");
           }
           //电话号校验
            if (!phoneValidate(bsMember.getMemberPhone())){
                throw new RRException("电话号不合法");
            }
            //生成唯一会员编号  uuid+时间戳
            String time = String.valueOf(new Date().getTime());
            String uuid = UUID.randomUUID().toString().replace("-","");
            String memberId = time + uuid ;
            bsMember.setMemberId(memberId);
            bsMemberDao.insert(bsMember);
        }catch (Exception e){
            log.info("报错类及所在方法：{}","BsMemberService --> savaMember");
            log.info(e.getMessage());
            throw new RRException(e.getMessage());
        }

    }

    @Override
    public void updateMemberById(BsMemberEntity bsMember) {
        try {
            //非空性校验
            if (StringUtils.isBlank(bsMember.getMemberName())){
                throw new RRException("会员姓名不能为空");
            }
            if (StringUtils.isBlank(bsMember.getMemberPhone())){
                throw new RRException("会员电话不能为空");
            }
            if (StringUtils.isBlank(bsMember.getMemberSex())){
                throw new RRException("会员性别不能为空");
            }
            if (bsMember.getMemberAge() == null){
                throw new RRException("会员年龄不能为空");
            }
            if (StringUtils.isBlank(bsMember.getMemberIdnum())){
                throw new RRException("会员身份证号不能为空");
            }
            if (bsMember.getMemberIdnum().length() != 18){
                throw new RRException("身份证号长度不合法");
            }
            //电话号校验
            if (!phoneValidate(bsMember.getMemberPhone())){
                throw new RRException("电话号不合法");
            }
           bsMemberDao.updateById(bsMember);
        }catch (Exception e){
            log.info("报错类及所在方法：{}","BsMemberService --> updateMemberById");
            log.info(e.getMessage());
            throw new RRException(e.getMessage());
        }
    }

    //校验电话号公共方法
    private boolean phoneValidate(String phoneNumber){
        if (phoneNumber != null && !phoneNumber.isEmpty()){
            boolean matches = Pattern.matches(
                    "^1[3-9]\\d{9}$|" +
                            "^1[3-9]\\d{1}[-\\s]\\d{4}[-\\s]\\d{4}$|" +
                            "^\\(1[3-9]\\d{1}\\)\\d{4}-\\d{4}$|" +
                            "^(?:\\(\\+\\d{2}\\)|\\+\\d{2})(\\d{11})$|" +
                            "^0\\d{3}-\\d{7}$|" +
                            "^0\\d{2}-\\d{8}$", phoneNumber);

            return matches;
        }
        return false;
    }


}
